import pandas as pd
from dao.RecordDao import RecordDao
from util import CommonHelper
from Static import Static
from StatusCode import StatusCode
from util import Logger
from util.Rater import Rater
import json


class RecordService:
    def initDB(self):
        dao = RecordDao()
        record = {
            "patient_id": 1,
            "dept_id": 2,
            "doctor_id": 1,
            "condition": "测试主诉",
            "present_illness": "测试现病史",
            "first_visit": "2023-01-01",
            "disease": "[]",
        }
        df = pd.DataFrame(data=[record], columns=record.keys())
        dao.importRecords(df)

    def getRecords(self, json_data):
        patientName = json_data.get("patientName", "").strip()
        deptId = json_data.get("dept_id", "").strip()
        doctorName = json_data.get("doctorName", "").strip()
        return RecordDao().getRecords(patientName, deptId, doctorName)

    def getRecordInfo(self, json_data):
        recordId = json_data.get("recordId", -1)
        df = RecordDao().getRecordInfo(recordId)
        df["disease"] = df["disease"].apply(json.loads)
        data = df.to_dict(orient="records")[0]
        data["report"] = Rater().rateDisease(data["disease"])
        return data

    # 新增患者
    def addRecord(self, json_data):
        try:
            Logger.logger(json_data)
            disease = json.dumps(json_data.get("disease", "[]"), ensure_ascii=False)
            Logger.logger(disease)
            first_visit = json_data.get("first_visit", "")
            if first_visit:
                first_visit = CommonHelper.parseUTCTime(first_visit)
            record = {
                "patient_id": json_data.get("patientId", -1),
                "dept_id": json_data.get("dept_id", -1),
                "doctor_id": json_data.get("doctorId", -1),
                "condition": json_data.get("condition", "-").strip(),
                "present_illness": json_data.get("present_illness", "-").strip(),
                "first_visit": first_visit,
                "disease": disease,
            }
            df = pd.DataFrame(data=[record], columns=record.keys())
            RecordDao().importRecords(df)
            return StatusCode.RESPONSE_SUCCESS
        except Exception as e:
            print(e)
            return StatusCode.RESPONSE_FAIL

    def delRecord(self, json_data):
        recordId = json_data.get("recordId", -1)
        if recordId == -1:
            return StatusCode.RESPONSE_FAIL
        RecordDao().delRecord(recordId)
        return StatusCode.RESPONSE_SUCCESS

    def updateRecord(self, json_data):
        Logger.logger(json_data)
        recordId = json_data.get("id", -1)
        if recordId == -1:
            return StatusCode.RESPONSE_FAIL
        patientId = json_data.get("patient_id", -1)
        deptId = json_data.get("dept_id", -1)
        doctorId = json_data.get("doctor_id", -1)
        outpatientCode = json_data.get("outpatient_code", "-").strip()
        inpatientCode = json_data.get("inpatient_code", "-").strip()
        first_visit = json_data.get("first_visit", "").strip()
        if first_visit:
            first_visit = CommonHelper.parseUTCTime(first_visit)
        disease = json.dumps(json_data.get("disease", "[]"), ensure_ascii=False)
        condition = json_data.get("condition", "-").strip()
        present_illness = json_data.get("present_illness", "-").strip()
        RecordDao().updateRecord(
            recordId,
            patientId,
            deptId,
            doctorId,
            outpatientCode,
            inpatientCode,
            first_visit,
            condition,
            present_illness,
            disease,
        )
        return StatusCode.RESPONSE_SUCCESS
